package org.kailu.book.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.kailu.book.entity.BookEntity;
import org.kailu.book.entity.DepartmentEntity;
import org.kailu.book.entity.ExportBookDetailsEntity;
import org.kailu.book.entity.ExportEntity;
import org.kailu.book.entity.ExportStationeryDetailsEntity;
import org.kailu.book.entity.StationeryEntity;
import org.kailu.book.service.BookService;
import org.kailu.book.service.DepartmentService;
import org.kailu.book.service.ExportBookDetailsService;
import org.kailu.book.service.ExportService;
import org.kailu.book.service.ExportStationeryDetailsService;
import org.kailu.book.service.StationeryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class ExportController {

	@Autowired
	private ExportService exportService;
	@Autowired
	private BookService bookService;
	@Autowired
	private StationeryService stationService;
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private ExportBookDetailsService bdService;
	@Autowired
	private ExportStationeryDetailsService sdService;

	@RequestMapping("/export")
	public String getData(ModelMap map) {
		map.addAttribute("exportList", exportService.getAll());
		return "export";
	}

	@RequestMapping("/export/new")
	public String newExport(ModelMap map) {
		map.addAttribute("bookList", bookService.getAll());
		map.addAttribute("stationeryList", stationService.getAll());
		map.addAttribute("departmentList", departmentService.getAll());
		map.addAttribute("export", new ExportEntity());
		map.addAttribute("book_details", new ExportBookDetailsEntity());
		map.addAttribute("stationery_details",
				new ExportStationeryDetailsEntity());
		return "export_new";
	}

	public StringBuilder updateDatabase(int billId, int isExport, int verify) {

		StringBuilder errorMess = new StringBuilder();

		// get data
		List<ExportBookDetailsEntity> bookList = bdService
				.getByExportId(billId);
		List<ExportStationeryDetailsEntity> StationeryList = sdService
				.getByExportId(billId);

		// update for stationery
		for (ExportStationeryDetailsEntity billStationeryDetailsEntity : StationeryList) {
			StationeryEntity se = stationService
					.get(billStationeryDetailsEntity.getStationery()
							.getStationeryId());

			if (se.getStationeryQuantity()
					- billStationeryDetailsEntity.getQuantity() < 0) {
				errorMess
						.append((billStationeryDetailsEntity.getStationery()
								.getStationeryName() + " không đủ số lượng để xuất <p></p>"));
				System.out.println("NOT ENOUGH!");
				System.out.println(errorMess);
			}
		}

		// update for book
		for (ExportBookDetailsEntity billBookDetailsEntity : bookList) {
			BookEntity se = bookService.get(billBookDetailsEntity.getBook()
					.getBookId());

			if (se.getBookQuantity() - billBookDetailsEntity.getQuantity() < 0) {
				errorMess
						.append((billBookDetailsEntity.getBook().getBookName() + " không đủ số lượng để xuất <p></p>"));
			}
		}
		


			if (errorMess != null && !errorMess.toString().equals("")) {

				for (ExportStationeryDetailsEntity billStationeryDetailsEntity : StationeryList) {
					sdService.delete(billStationeryDetailsEntity.getId());
				}

				// update for book
				for (ExportBookDetailsEntity billBookDetailsEntity : bookList) {
					bdService.delete(billBookDetailsEntity.getId());
				}

				exportService.delete(billId);
			} else {

			if (verify == 1) {
				// update for stationery
				for (ExportStationeryDetailsEntity billStationeryDetailsEntity : StationeryList) {
					StationeryEntity se = stationService
							.get(billStationeryDetailsEntity.getStationery()
									.getStationeryId());

					se.setStationeryQuantity(se.getStationeryQuantity()
							- billStationeryDetailsEntity.getQuantity());
					stationService.saveOrUpdate(se);
				}

				// update for book
				for (ExportBookDetailsEntity billBookDetailsEntity : bookList) {
					BookEntity se = bookService.get(billBookDetailsEntity
							.getBook().getBookId());

					se.setBookQuantity(se.getBookQuantity()
							- billBookDetailsEntity.getQuantity());
					bookService.saveOrUpdate(se);
				}
			}

		}

		return errorMess;
	}

	@RequestMapping(value = "/export/saveorupdate", method = RequestMethod.POST)
	public String save(HttpServletRequest request, ModelMap map) {
		DepartmentEntity department = departmentService.get(Integer
				.parseInt((request.getParameter("departmentId").toString())));
		Date date = new Date();
		int verify = 1;

		String sReturn = null;

		ExportEntity export = new ExportEntity(department, date, verify);

		exportService.save(export);

		// insert book
		for (int i = 1; i < 20; i++) {

			if (request.getParameter("bookId" + i) == null) {
				continue;
			} else {
				int bookId = Integer.parseInt(request
						.getParameter("bookId" + i));
				int bookQuantity = Integer.parseInt(request
						.getParameter("bookQuantity" + i));

				BookEntity bookEn = bookService.get(bookId);
				bdService.save(new ExportBookDetailsEntity(bookEn, export,
						bookQuantity));
			}
		}

		// insert stationery
		for (int i = 1; i < 20; i++) {

			String stationeryIdName = "stationeryId" + i;

			if (request.getParameter("stationeryId" + i) == null) {

				System.out.println("breake: " + stationeryIdName);

				continue;
			} else {
				int stationeryId = Integer.parseInt(request
						.getParameter("stationeryId" + i));
				int stationeryQuantity = Integer.parseInt(request
						.getParameter("stationeryQuantity" + i));
				StationeryEntity stationeryEn = stationService
						.get(stationeryId);
				sdService.save(new ExportStationeryDetailsEntity(export,
						stationeryEn, stationeryQuantity));
			}
		}

		StringBuilder errorMess = updateDatabase(export.getExportId(), 1, 1);

		if (errorMess != null && !errorMess.toString().equals("")) {
			map.addAttribute("bookList", bookService.getAll());
			map.addAttribute("stationeryList", stationService.getAll());
			map.addAttribute("departmentList", departmentService.getAll());
			map.addAttribute("export", new ExportEntity());
			map.addAttribute("book_details", new ExportBookDetailsEntity());
			map.addAttribute("stationery_details",
					new ExportStationeryDetailsEntity());
			map.addAttribute("errorMess", errorMess);

			System.out.println("MESSAGE" + errorMess);

			sReturn = "export_new";
		} else {
			sReturn = "redirect:/export/" + export.getExportId();
		}

		return sReturn;

	}

	@RequestMapping(value = "/export/{exportId}", method = RequestMethod.GET)
	public String fillExport(@PathVariable("exportId") int export_id,
			ModelMap map) {

		List<ExportBookDetailsEntity> bookList = bdService
				.getByExportId(export_id);
		List<ExportStationeryDetailsEntity> StationeryList = sdService
				.getByExportId(export_id);

		map.addAttribute("department", exportService.get(export_id)
				.getDepartment());
		map.addAttribute("bookList", bookList);
		map.addAttribute("stationeryList", StationeryList);
		map.addAttribute("export", exportService.get(export_id));
		return "export_invoice";
	}
}
